package cn.zhibang.sannuo.repoty.api;

import cn.zhibang.sannuo.commons.model.BranchOffice;
import cn.zhibang.sannuo.commons.model.Ccudetails;
import cn.zhibang.sannuo.commons.model.CommonResult;
import cn.zhibang.sannuo.commons.model.Customer;
import cn.zhibang.sannuo.repoty.service.RcustomerServices;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@Slf4j
public class ReportApi {

    @Autowired
    private RcustomerServices rcustomerServices;

    @Autowired private CommonResult commonResult;

    /**
     * 指标总览
     * @return
     */
    @PostMapping("/getExaminer")
    public CommonResult getExaminer(String cname, String cphone, String bname, String hname){
        if (cname==""){
            cname = null;
        }
        if ("全部".equals(hname)){
            hname = null;
        }
        if (cphone==""){
            cphone = null;
        }
        if ("全部".equals(bname)){
            bname = null;
        }
        List<Customer> listIndexSum = rcustomerServices.selIndexSum(cname,cphone,bname,hname);
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        map.put("listIndexSum", listIndexSum);
        commonResult.setData(map);
        return commonResult;
    }

    /**
     * 消费者检查频次排行
     * @return
     */
    @PostMapping("/getCcudetails")
    public CommonResult getCustomer(){
        List<Ccudetails> ccudetailsList = rcustomerServices.queryConsumerCount();
        commonResult.setData(ccudetailsList);
        return commonResult;
    }

//    总店下各分店的总人数排名
    @PostMapping("/getBnameSumInHname")
    public CommonResult getBnameSumInHname(){
        List<BranchOffice> bnameInHname = rcustomerServices.queryBnameInHname();
        List<Customer> bnameSum = rcustomerServices.queryBnameSum();

        //异常率最高单店
        List<Map<String, Object>> queryMityPeople = rcustomerServices.queryIndexAnomaly();
        Map<String, Object> maxMityBname = queryMityPeople.get(0);

        List<Map<String, Object>> queryCountByBname = rcustomerServices.queryCountByBname();
        Map<String, Object> bnameCountMax = queryCountByBname.get(0);

        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        map.put("queryMityPeople", queryMityPeople);
        map.put("maxMityBname", maxMityBname);
        map.put("queryCountByBname", queryCountByBname);
        map.put("bnameCountMax", bnameCountMax);
        commonResult.setData(map);
        return commonResult;
    }

//    总店下所有分店option
    @PostMapping("/bnameHnameOption")
    public CommonResult bnameHnameOption(String hname){
        if("全部".equals(hname))hname=null;
        List<BranchOffice> hnameContentBname = rcustomerServices.queryHnameContentBname(hname);
        commonResult.setData(hnameContentBname);
        return commonResult;
    }


}
